﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace EmoneyWeiXinCommon
{
    public class SendMail
    {
        private string _mailFrom = "";
        private string _userName = "";
        private string _userPwd = "";
        private string _host = "";
        private string _port = "";
        private string _displayname = "";

        public SendMail()
        {
            this._mailFrom = Config.mailFrom;
            this._userName = Config.userName;
            this._userPwd = Config.userPwd;
            this._host = Config.host;
            this._port = Config.port;
            this._displayname = Config.displayname;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailModel">必要设置属性：MailTo,Subject,Body,IsBodyHtml</param>
        public bool Send()
        {
            MailMessage mail = new MailMessage();
            MailAddress from = new MailAddress(_mailFrom, _displayname);
            mail.From = from;

            if (this.Attachments != null)
            {
                for (int i = 0; i < Attachments.Count; i++)
                {
                    mail.Attachments.Add(Attachments[i]);
                }
            }

            for (int i = 0; i < MailTo.Length; i++)
            {
                mail.To.Add(new MailAddress(MailTo[i]));
            }

            if (CC != null)
            {
                for (int i = 0; i < CC.Length; i++)
                {
                    mail.CC.Add(new MailAddress(CC[i]));
                }
            }

            mail.Subject = Subject;
            mail.Body = Body;

            mail.IsBodyHtml = true;

            mail.Priority = Priority;

            SmtpClient smtp = new SmtpClient(_host, Convert.ToInt32(_port));
            NetworkCredential credential = new NetworkCredential();
            credential.UserName = _userName;
            credential.Password = _userPwd;
            mail.SubjectEncoding = Encoding.UTF8;
            mail.BodyEncoding = Encoding.UTF8;
            smtp.Credentials = credential;
            try
            {
                smtp.Send(mail);
                return true;
            }
            catch (Exception)
            {
                throw;
            }
            //mail.Dispose();

        }
        //收件人
        public string[] MailTo { get; set; }
        //主题
        public string Subject { get; set; }
        //内容
        public string Body { get; set; }
        //可选，邮件优先级
        private MailPriority _priority = MailPriority.Normal;
        public MailPriority Priority
        {
            get { return this._priority; }
            set { this._priority = value; }
        }
        //可选，抄送
        public string[] CC { get; set; }

        public List<Attachment> Attachments { get; set; }

    }
}
